package com.jspgou.cms.action.admin.main;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.common.web.ResponseUtils;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.LogMng;
import com.jspgou.core.web.WebErrors;
import com.jspgou.cms.entity.Poster;
import com.jspgou.cms.manager.PosterMng;
import com.jspgou.cms.web.SiteUtils;

@Controller
public class PosterAct {
	private static final Logger log = LoggerFactory.getLogger(PosterAct.class);

	@RequestMapping("/poster/v_list.do")
	public String list(Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		List<Poster> listPoster=manager.getPage();
		model.addAttribute("list", listPoster);
		model.addAttribute("uploadRoot", SiteUtils.getWeb(request).getUploadUrl());
		return "poster/list";
	}
	
	@RequestMapping("/poster/v_add.do")
	public String add(HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		model.addAttribute("uploadRoot", web.getUploadUrl());
		return "poster/add";
	}
	
	@RequestMapping("/poster/v_edit.do")
	public String edit(Long id, HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		model.addAttribute("uploadRoot", web.getUploadUrl());
		model.addAttribute("poster", manager.findById(id));
		return "poster/edit";
	}
	
	@RequestMapping("/poster/o_save.do")
	public String save(Poster bean, 
			HttpServletRequest request, ModelMap model) {
		bean = manager.saveOrUpdate(bean);
		log.info("save brand. id={}", bean.getId());
		logMng.operating(request, "添加首页海报", "id="+bean.getId()+" ;picture="+bean.getPicUrl());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/poster/o_update.do")
	public String update(Poster bean, 
			Integer pageNo, HttpServletRequest request, ModelMap model) {
		bean = manager.update(bean);
		log.info("update brand. id={}.", bean.getId());
		logMng.operating(request, "修改首页海报", "id="+bean.getId()+" ;picture="+bean.getPicUrl());
		return list(pageNo, request, model);
	}
	
//	@RequestMapping("/poster/o_updateCare.do")
//	public String o_updateCare(String val,Integer id, HttpServletRequest request,HttpServletResponse response,
//			ModelMap model) {
//		Poster bean=manager.findById(id);
//		bean.setInterUrl(val);
//		manager.update(bean);
//		ResponseUtils.renderJson(response, "success");
//		return null;
//	}
//	
//	@RequestMapping("/poster/o_update.do")
//	public String edit(String[] picUrl,String[] interUrl, Integer pageNo,
//			HttpServletRequest request,ModelMap model) {
//		if(picUrl!=null && picUrl.length>0){
//			for(int i=0;i<picUrl.length;i++){
//				Poster p=new Poster();
//				p.setTime(new Date());
//				p.setPicUrl(picUrl[i]);
//				p.setInterUrl(interUrl[i]);
//				manager.saveOrUpdate(p);
//			}
//		}
//		return "redirect:v_list.do";
//	}
	
	@RequestMapping("/poster/o_delete.do")
	public String delete(Long[] ids, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
	      manager.deleteByIds(ids);
	      for(Long id:ids){
	    	  Poster bean=manager.findById(id);
	      }
		return list(pageNo, request, model);
	}
	
	private WebErrors validateDelete(Long[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Long id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}
	
	private boolean vldExist(Long id, WebErrors errors) {
		Poster entity = manager.findById(id);
		return errors.ifNotExist(entity, Poster.class, id);
	}

	@RequestMapping("/poster/o_delPoster.do")
	public String delete(Long id, Integer pageNo,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		manager.deleteById(id);
		ResponseUtils.renderJson(response, "success");
		return null;
	}
	
	@Autowired
	private PosterMng manager;
	@Autowired
	private LogMng logMng;
	
}